package it.hansir.sonic_tbs;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final int PERMISSION_REQUEST_CODE_STORAGE = 1001;
    private boolean perTag = true;
    private Button button,nextB;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button);
        nextB = (Button) findViewById(R.id.button2);
        button.setOnClickListener(this);
        if (hasPermission()) {
        }else {
            requestPermission();
        }

    }


    /**
     * 判断权限
     * @return
     */
    public boolean hasPermission() {
        LogUtil.d(this,"hasPermission");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            LogUtil.d(this,"hasPermission判断是否有");
            return ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE) ==
                    PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this,Manifest.permission.GET_ACCOUNTS) == PackageManager.PERMISSION_GRANTED;
        }
        return true;
    }

    /**
     * 获取权限
     */
    private void requestPermission() {
        LogUtil.d(this,"requestPermission");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            LogUtil.d(this,"requestPermission请求权限");
            requestPermissions(new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.GET_ACCOUNTS}, PERMISSION_REQUEST_CODE_STORAGE);
        }
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                Intent intent = new Intent(this,WebActivity.class);
                startActivity(intent);
                break;
            case R.id.button2:

                break;
        }
    }
}
